package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	//获取所有专辑分类数据
	@Override
	public List<JSONObject> getBaseCategoryList() {
		//1.要返回的JSON对象
		List<JSONObject> oneBaseCategoryList = new ArrayList<>();
		//2.获取数据源
		List<BaseCategoryView> dataSource = baseCategoryViewMapper.selectList(null);

		//3.对数据源进行分组,对一级分类id进行分组;结果用Map装,key是分组字段,value是这一组的所有记录
		Map<Long, List<BaseCategoryView>> groupByOneIdMap = dataSource.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//4.对分组后的数据进行提取,提取成JSONObject对象,然后塞到return里;即:遍历Map
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iteratorOne = groupByOneIdMap.entrySet().iterator();
		while (iteratorOne.hasNext()) {
			//5.1从迭代器中获取一个Map的结点
			Map.Entry<Long, List<BaseCategoryView>> entry = iteratorOne.next();
			//5.2从Map结点获取key和value;key就是一级分类id,value是这个id下的所有记录
			Long oneId = entry.getKey();
			List<BaseCategoryView> groupByOneIdMapValueList = entry.getValue();

			//6.从数据源获取数据,提取到JSONObject里
			JSONObject oneBaseCategoryInfo = new JSONObject();

			//6.1提取id
			oneBaseCategoryInfo.put("categoryId", oneId);
			//6.2提取name,因为name都一样,所以用第一条记录的name即可
			oneBaseCategoryInfo.put("categoryName", groupByOneIdMapValueList.get(0).getCategory1Name());
			//6.3提取二级分类信息,这时数据源从所有记录缩小到了当前一级分类下的所有记录,即groupByOneIdMapValue
			List<JSONObject> twoBaseCategoryList = new ArrayList<>();

			//6.3.1对一级分类信息再次分类,这些信息同属于一个一级分类;结果用Map装,key是分组字段,value是这一组的所有记录
			Map<Long, List<BaseCategoryView>> groupByTwoIdMap = groupByOneIdMapValueList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//6.3.2对分组后的数据进行提取,提取成JSONObject对象,然后塞到 twoBaseCategoryList 里;即:遍历Map
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iteratorTwo = groupByTwoIdMap.entrySet().iterator();
			while (iteratorTwo.hasNext()) {
				Map.Entry<Long, List<BaseCategoryView>> entryTwo = iteratorTwo.next();

				//6.3.3获取key和value
				Long twoId = entryTwo.getKey();
				List<BaseCategoryView> groupByTwoIdMapValueList = entryTwo.getValue();

				//6.3.4从数据源获取数据,提取到JSONObject里
				JSONObject twoBaseCategoryInfo = new JSONObject();
				//6.3.4.1将id和name塞到JSONObject里
				twoBaseCategoryInfo.put("categoryId", twoId);
				twoBaseCategoryInfo.put("categoryName", groupByTwoIdMapValueList.get(0).getCategory2Name());

				//6.3.4.2将三级分类信息从数据源提取,直接将整个List<BaseCategoryView>数据源提取成List<JSONObject>
				Stream<JSONObject> threeBaseCategoryList = groupByTwoIdMapValueList.stream().map(baseCategoryViewForThree -> {
					JSONObject threeBaseCategoryInfo = new JSONObject();
					threeBaseCategoryInfo.put("categoryId", baseCategoryViewForThree.getCategory3Id());
					threeBaseCategoryInfo.put("categoryName", baseCategoryViewForThree.getCategory3Name());
					return threeBaseCategoryInfo;
				});
				//6.3.4.3将三级分类信息塞到JSONObject里
				twoBaseCategoryInfo.put("categoryChild", threeBaseCategoryList);

				//6.3.5将单个二级分类信息放到其从属的一级分类下
				twoBaseCategoryList.add(twoBaseCategoryInfo);
			}

			oneBaseCategoryInfo.put("categoryChild", twoBaseCategoryList);
			//7.将单个一级分类信息放到要return的List里
			oneBaseCategoryList.add(oneBaseCategoryInfo);
		}
		return oneBaseCategoryList;
	}
}
